package com.heima.comment.istener;

import com.alibaba.fastjson.JSON;
import com.heima.comment.service.CommentRepayService;
import com.heima.comment.service.CommentService;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentListDto;
import com.heima.model.wemedia.pojos.WmUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author JianKang
 * @create 2023-11-07 13:16
 */
@Component
@Slf4j
public class ApCommentListener {

    @Autowired
    CommentRepayService commentRepayService;
    @Autowired
    CommentService commentService;

    @KafkaListener(topics = "comment-repay")
    public void commentRepay(String message) {
        log.info(message);

        Map map = JSON.parseObject(message, Map.class);
        WmUser wmUser = JSON.parseObject(map.get("wmUser").toString(), WmUser.class);
        String commentId = map.get("commentId").toString();
        String content = map.get("content").toString();
        commentRepayService.saveWmCommentRepay(wmUser, commentId, content);
    }

    @KafkaListener(topics = "comment-like")
    public void commentLike(String message) {
        log.info(message);

        Map map = JSON.parseObject(message, Map.class);
        WmUser wmUser = JSON.parseObject(map.get("wmUser").toString(), WmUser.class);
        CommentLikeDto dto = JSON.parseObject(map.get("dto").toString(), CommentLikeDto.class);
        commentRepayService.commentLike(wmUser, dto.getCommentId(), dto.getOperation());
    }
}
